package leetcode;

/**
 * @author Liangyaoyun
 */
public class leetCode110 {

    public class TreeNode {
        int val;
        TreeNode left;
        TreeNode right;
        TreeNode() {}
        TreeNode(int val) { this.val = val; }
        TreeNode(int val, TreeNode left, TreeNode right) {
            this.val = val;
            this.left = left;
            this.right = right;
        }
        }
    public boolean isBalanced(TreeNode root) {
        int leftdeep=0;
        int rightdeep=0;
        //判空，如果不判空下面的左右子树节点将会报错
        if(root==null){
            return true;
        }
        int res=depth(root.left,leftdeep)-depth(root.right,rightdeep);
        res= Math.abs(res);
        if (res==1||res==0){
            return true;
        }else{
            return false;
        }
    }
    public int depth(TreeNode root,int deep){

        if (root==null){
            return deep;
        }
        deep++;
        if (root.left!=null){
            return depth(root.left,deep);
        }
        else {
            return depth(root.right,deep);
        }

    }
}
